﻿var itemsFailed = false;
var map = null;
var resultsArray = null;
var serverUrl = "";


if (window.opener != null && window.opener.resultsArray != null)
    resultsArray = window.opener.resultsArray;

function GetMap() {
    var context = GetGlobalContext();
    serverUrl = context.getServerUrl();
    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);
    }
    map = new VEMap('myMap');
    map.LoadMap();

    var lastItem = resultsArray.length

    if (resultsArray != null && resultsArray != undefined) {
        for (var i = 0; i < lastItem; i++) {
            resultsArray[i].id = i;
            FindLocation(resultsArray[i]);
        }
    }
}

function FindLocation(entity) {
    if (entity != null && entity != undefined) {
        var moreUrl = serverUrl + "/main.aspx?etn=" + entity.Type + "&pagetype=entityrecord&id=" + entity.Id;
        var address = "";
        if (entity.Street != null && entity.Street != "")
            address += entity.Street + ", ";

        if (entity.City != null && entity.City != "")
            address += entity.City + ", ";

        if (entity.State != null && entity.State != "")
            address += entity.State + ", ";

        if (entity.Country != null && entity.Country != "")
            address += entity.Country;

        address = address.trimEnd(" ");
        address = address.trimEnd(",");

        if (entity.Latitude != null && entity.Longitude != null) {
            AddPin(entity.Name, address, moreUrl, new VELatLong(entity.Latitude, entity.Longitude));
        }
        else {
            map.Find(null, address, null, null, 0, 1, false, false, true, false, function (shapeLayer, findresults, places, moreresults, error) {
                if ((error != null && error != "") || places == null || places.length < 1) {
                    AddInvalidAddressObject(entity, address, moreUrl);
                    if (itemsFailed == false) {
                        itemsFailed = true;
                    }
                    return;
                }
                AddPin(entity.Name, address, moreUrl, places[0].LatLong);
            });
        }

        //Work on this later, don't use in current release
        /*
        if (entity.Shape != null && entity.Shape != "") {
            var latLongs = new Array();
            var coords = entity.Shape.split(',');

            for (var k = 0; k < coords.length; k++) {
                var latLong = coords[k].split(':');
                latLongs[k] = new VELatLong(latLong[0], latLong[1]);
            }

            var area = new VEShape(VEShapeType.Polygon, latLongs);

            area.SetTitle(entity.Name);
            area.SetDescription(address);
            if (moreUrl != null && moreUrl != "")
                area.SetMoreInfoURL(moreUrl);


            //Add the shape the the map
            map.AddShape(area);
            
        }*/
    }
}

function AddPin(name, description, moreInfoUrl, latLong) {
    var pin = new VEShape(VEShapeType.Pushpin, latLong);
    pin.SetTitle(name);
    pin.SetDescription(description);
    if(moreInfoUrl != null && moreInfoUrl != "")
        pin.SetMoreInfoURL(moreInfoUrl);
    map.AddShape(pin);
    map.SetCenterAndZoom(latLong, 10);
}

function AddInvalidAddressObject(entity, address, url) {

    var invalidAddressTable = document.getElementById("invalidAddresses");

    var newRow = document.createElement("tr");

    var dataName = document.createElement("td");

    var href = document.createElement("a");
    if (entity.Name != null && entity.Name != "")
        href.innerText = entity.Name;
    else
        href.innerText = "[No name found]";

    href.setAttribute("href", url);
    href.setAttribute("target", "_blank");
    dataName.appendChild(href);

    newRow.appendChild(dataName);

    var dataAddress = document.createElement("td");
    dataAddress.innerText = address;
    newRow.appendChild(dataAddress);

    invalidAddressTable.childNodes[0].appendChild(newRow);
}

String.prototype.trimEnd = function (c) {
    c = c ? c : ' ';
    var i = this.length - 1;
    for (; i >= 0 && this.charAt(i) == c; i--);
    return this.substring(0, i + 1);
}